//
//  CertificateMailController.m
//  VideoExam
//
//  Created by apple on 2020/4/17.
//

#import "CertificateMailController.h"
#import "CertificateHeaderView.h"
#import "CertificateMailModel.h"
#import "CertificateMailCell.h"
#import "CityPickerVeiw.h"
@interface CertificateMailController ()
@property(nonatomic,strong)CertificateHeaderView * headerView;
@property(nonatomic,strong)CertificateMailModel * model;
@property(nonatomic,assign)BOOL editor;//yes 可编辑
@property(nonatomic,strong)UIView * bottomView;
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,strong)UIButton * submit;
@end

@implementation CertificateMailController
- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}
- (void)setupNavigationItems {
    [super setupNavigationItems];
    [self isShowNavBack_pop];
    self.title=@"电子证书";
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    NSLog(@"当前订单号 %@",self.source_model.);
//    [QMUITips showLoadingInView:self.view];
    
    //获取邮寄地址信息
    [CertificateMailModel  get_order_address_info_orderId:self.detailsModel.orderId Success:^(id obj) {
        self.model = obj;
        [self data_configuration];
    } error:^{
    }];

}
- (void)init_data {
    self.dataArray = [NSMutableArray new];
//    NSArray * titles = @[@"联系电话",@"考生姓名",@"性别",@"国籍",@"民族",@"身份证",@"出生日期",];
     NSArray *placeholders = @[@"请填写姓名",@"请填写联系电话",@"请选择性别城市",@"请选填写详细地址",];
    NSArray * errors = @[@"姓名错误",@"联系电话不正确",@"请选择城市",@"详细地址不可为空",];
    for (int i= 0; i<placeholders.count; i++) {
        ExamineeInfoCellModel * model = [[ExamineeInfoCellModel alloc] init];
        model.cellHeight = 60;
//        model.title =  titles[i];
        model.row = i;
        model.placeholder = placeholders[i];
        model.errorInfo = errors[i];
        if (i == 2) {
            model.selected = YES;
        }
        if (i==0) {
             model.content = self.model.name ;
        } else if (i==1) {
             model.content = self.model.phone;
        } else if (i==2) {
             model.content = self.model.city;
        } else if (i==3) {
             model.content = self.model.address;
        }
        [self.dataArray addObject:model];
    }
}
- (void)data_configuration {
    if (self.model) {
        if (self.model.status == 6) {//等于6时可编辑 填写地址状态
            self.editor = YES;
        } else {
            self.editor = NO;
        }
    } else {
        self.editor = YES;
        self.model = [[CertificateMailModel alloc] init];
        self.model.name = self.detailsModel.name;
        self.model.phone = self.detailsModel.phone;
    }
    self.model.orderId = self.detailsModel.orderId;
    [self init_data];
    [self data_detection];
    [self.tableView reloadData];
 
}

- (void)initSubviews {
    [super initSubviews];
}
- (void)setEditor:(BOOL)editor {
    _editor=editor;
    if (_editor) {
        [self addBottomView];
        [self layoutTableView];
    }
}

- (void)initTableView {
    [super initTableView];
//    self.tableView.backgroundColor=APPGrayColor;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView.tableHeaderView = self.headerView;
}
- (void)layoutTableView {
    [super layoutTableView];
    CGFloat height =  SCREEN_HEIGHT-BottomSafeHeight;
    if (self.editor) {
        height =  SCREEN_HEIGHT-BottomSafeHeight-65;
    }
    self.tableView.frame=CGRectMake(0, 0,SCREEN_WIDTH,height);
}

- (CertificateHeaderView *)headerView {
    if (_headerView==nil) {
        _headerView=[[CertificateHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 220)];
        _headerView.model =self.detailsModel;
    }
    return _headerView;
}
#pragma mark - <QMUITableViewDataSource, QMUITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editor) {
        ExamineeInfoCellModel *model = self.dataArray[indexPath.row];
        return model.cellHeight;
    }
    if (indexPath.row==3) {
        return self.model.cellHeight;
    }
    return 60;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.editor==NO) {
        static NSString *identifier = @"CertificateMailShowCell";
       CertificateMailShowCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
       if (!cell) {
           cell = [[CertificateMailShowCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
           cell.selectionStyle=UITableViewCellSelectionStyleNone;
       }
        cell.indexPath=indexPath;
        @weakify(self);
        [cell setCellHeightBlock:^(CGFloat cellHeight) {
            @strongify(self);
            self.model.cellHeight=cellHeight;
        }];
         if (indexPath.row==0) {
                cell.content=self.model.name;
           } else if (indexPath.row==1) {
                cell.content=self.model.phone;
           } else if (indexPath.row==2) {
                cell.content=self.model.city;
           } else {
               cell.content=self.model.address;
           }
        return cell;
    }
    
    static NSString *identifier = @"CertificateMailCell";
    CertificateMailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CertificateMailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    cell.tableView = tableView;
    cell.model = self.dataArray[indexPath.row];
    @weakify(self);
    [cell setData_detection_block:^{
       @strongify(self);
        [self data_detection];
    }];
    return cell;
}
#pragma mark 数据检测
- (void)data_detection {
    for (int i=0; i<self.dataArray.count; i++) {
         ExamineeInfoCellModel *model =self.dataArray[i];
           if (i==0) {
               self.model.name = model.content;
           } else if (i==1) {
               self.model.phone = model.content;
           } else if (i==2) {
               self.model.city = model.content;
           } else if (i==3) {
               self.model.address = model.content;
           }
     }
    BOOL can_submit = [self.model data_validation];//yes 可保存
     self.submit.enabled = can_submit;
     if (can_submit) {
         self.submit.backgroundColor = theme_color;
     } else {
         self.submit.backgroundColor = color(@"#D9D9D9");
     }
}
#pragma mark addBottomView
- (void)addBottomView {
    self.bottomView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.bottomView];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-BottomSafeHeight);
        make.height.mas_equalTo(65);
    }];
    
    self.submit = [UIFactory create_btnWithTitle:@"提交" imageName:nil backgroundColor:theme_color TypefaceColor:[UIColor whiteColor] fontSize:16 target:self action:@selector(submissionClicked:)];
    self.submit.layer.cornerRadius=22;
    self.submit.layer.masksToBounds=YES;
    [self.bottomView addSubview:self.submit];
    [self.submit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(1);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(self.bottomView.mas_right).offset(-15);
        make.height.mas_equalTo(44);
    }];
}
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (self.editor==NO || indexPath.row==0) return;
//   
//}
- (void)submissionClicked:(UIButton *)btn {
    [PopupWindow showType:Show_address_type ClickBolck:^(NSInteger type) {
        if (type == 1) {
            btn.enabled=NO;
            [self submission];
        } else {
            btn.enabled=YES;
        }
    }];
}
- (void)submission {
    @weakify(self);
    [self.model add_addressSuccess:^(id obj) {
        @strongify(self);
        self.submit.enabled=YES;
        [QMUITips showSucceed:@"提交成功"];
        self.bottomView.hidden=YES;
        self.editor=NO;
        [self layoutTableView];
        [self.tableView reloadData];
    } error:^{
        @strongify(self);
        self.submit.enabled=YES;
    }];
}
@end
